package com.lkd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.constant.StatusConstant;
import com.lkd.dto.LoginDto;
import com.lkd.dto.UserDto;
import com.lkd.entity.Role;
import com.lkd.entity.User;
import com.lkd.exception.BusinessException;
import com.lkd.mapper.RoleMapper;
import com.lkd.mapper.UserMapper;
import com.lkd.result.PageBean;
import com.lkd.service.UserService;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    //用户登录
    @Override
    public User login(LoginDto dto) {
        // 1.参数校验
        String loginName = dto.getLoginName();
        String password = dto.getPassword();
        if (StrUtil.isBlank(loginName) || StrUtil.isBlank(password)) {
            throw new BusinessException("非法参数");
        }
        // 2.根据用户名查询数据库
        User user = userMapper.getByLoginName(loginName);

        // 3.业务校验
        // 3-1 用户是否存在
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }

        // 3-2 密码是否正确
        if (!StrUtil.equals(password, user.getPassword())) {
            throw new BusinessException("密码错误");
        }

        // 3-3 账号是否被禁用
        if (user.getStatus() == StatusConstant.DISABLE) {
            throw new BusinessException("此账号被禁用，请联系管理员");
        }
        // 4.登录成功
        return user;
    }

    @Override
    public Boolean save(User user) {
        //补齐参数
        Role role = roleMapper.findByRoleId(user.getRoleId());
        user.setRoleCode(role.getRoleCode());
        user.setPassword("123456");//默认密码
        user.setLoginName(user.getUserName());//默认登录名

        return userMapper.save(user);
    }

    @Override
    public PageBean<UserVo> findByPage(UserDto userDto) {
        PageHelper.startPage(userDto.getPageIndex(), userDto.getPageSize());

        List<UserVo> list = userMapper.findList(userDto);
        Page<UserVo> page = (Page<UserVo>) list;

        list.stream().forEach(userVo -> {
            Role role = roleMapper.findByRoleId(userVo.getRoleId());
            userVo.setRole(role);
        });

        PageBean<UserVo> pageBean = new PageBean<>();
        pageBean.setPageIndex(page.getPageNum())
                .setPageSize(page.getPageSize())
                .setTotalPage((long) page.getPages())
                .setTotalCount(page.getTotal())
                .setCurrentPageRecords(list);
        return pageBean;
    }

    @Override
    public Boolean update(User user) {
        //补齐参数
        Role role = roleMapper.findByRoleId(user.getRoleId());
        user.setRoleCode(role.getRoleCode());
        return userMapper.update(user);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return userMapper.deleteById(id);
    }
}
